package org.csu.ssm_backend.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.annotation.Resource;
import org.csu.ssm_backend.domain.Account;
import org.csu.ssm_backend.domain.Product;
import org.csu.ssm_backend.persistence.ProductMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@MapperScan("org.csu.ssm_backend.persistence")
public class CatelogService {
    @Resource
    private ProductMapper productMapper;

    public CatelogService(){
    }

    public  Product getProduct(String userId,String productId){
        QueryWrapper <Product>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productId).eq("userid",userId);
        return productMapper.selectOne(queryWrapper);
    }

    public List<Product> getProductList(String userId){
        QueryWrapper<Product> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",userId);
        return productMapper.selectList(queryWrapper);
    }

    public void insertProduct(Product product){
        productMapper.insert(product);
    }

    public void updateProduct(Product product){
        UpdateWrapper<Product> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("userid",product.getUserid()).eq("productid",product.getProductId());
        productMapper.update(product,updateWrapper);
    }

    public void deleteProduct(String userid,String productid){
        QueryWrapper <Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",userid).eq("productid",productid);
        System.out.println("DBMS:delete");
        System.out.println(userid+" "+productid);
        productMapper.delete(queryWrapper);
    }

    public List<Product> searchProduct(String userid,String keyword){
        QueryWrapper <Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",userid).like("name",keyword);
        return productMapper.selectList(queryWrapper);
    }

}
